<?

class i18n
{
	protected static $src = 'en';

	protected static $language = null;
	protected static $translations = array();

	public static function src_language()
	{
		if ( config::all()->i18n['src'] ) self::$src = config::all()->i18n['src'];
		return self::$src;
	}

	public static function translate( $phrase, $language = null )
	{
		if ( !$language ) $language = self::get_language();
		if ( $language == self::src_language() ) return $phrase;

		if ( !$language ) return $phrase;

		if ( !array_key_exists($language, self::$translations) )
		{
			if ( !self::$translations[$language] = self::get_translations($language) )
				self::set_language ( self::src_language() );
		}

		$translation = self::$translations[$language][$phrase];

		return $translation ? $translation : $phrase;
	}

	public static function get_translations( $language )
	{
		return include PROJECT_PATH . '/data/i18n/' . $language . '.php';
	}

	public static function update_translations( $language, $phrases )
	{
		$translations = self::get_translations($language);

		foreach ( $phrases as $phrase => $translation ) $translations[$phrase] = $translation;
		self::store_translations( $language, $translations );
	}

	public static function store_translations( $language, $translations )
	{
		$file = PROJECT_PATH . '/data/i18n/' . $language . '.php';

		$content = '<? return array(';
		foreach ( $translations as $phrase => $translation )
		{
			$phrase = str_replace("'", "\'", $phrase);
			$phrase = str_replace('\\\\', '\\', $phrase);
			$translation = str_replace("'", "\'", $translation);
			$content .= "'{$phrase}' => '{$translation}',\n";
		}
		$content .= ');';

		file_put_contents($file, $content);
	}

	public static function get_language()
	{
		if ( self::$language === null )
		{
			if ( !self::$language = session::get('language') )
			{
				$config = config::get('i18n');

				if ( !$config['force_default'] )
					self::$language = $_SERVER['HTTP_ACCEPT_LANGUAGE']{0} . $_SERVER['HTTP_ACCEPT_LANGUAGE']{1};

				if ( $config['languages'] && !in_array(self::$language, $config['languages']) ) self::$language = $config['default'];
				if ( !self::$language ) self::$language = false;

				session::set('language', self::$language);
			}
		}

		return self::$language;
	}

	public static function set_language( $lang )
	{
		self::$language = $lang;
		session::set('language', $lang);
	}

	public static function get_locale()
	{
		switch ( self::get_language() )
		{
			case 'en': return 'en_US'; break;
			case 'fr': return 'fr_FR'; break;
			case 'ru': return 'ru_RU'; break;
			case 'ua': return 'ua_UA'; break;
		}
	}
}

if ( !function_exists('_') )
{
	function _()
	{
		$arguments = func_get_args();

		$phrase = array_shift($arguments);
		$phrase = i18n::translate($phrase);

		array_unshift($arguments, $phrase);

		return call_user_func_array('sprintf', $arguments);
	}
}

function _field( $data, $field, $lang = null, $escape = true )
{
	if ( !is_array($data[$field]) ) return $escape ? htmlspecialchars ($data[$field]) : $data[$field];

	$lang = $lang ? $lang : i18n::get_language();
	
	if ( !$text = $data[$field][$lang] )
	{
		foreach ( config::all()->i18n['languages'] as $lang )
			if ( $text = $data[$field][$lang] ) break;
	}

	return $escape ? htmlspecialchars($text) : $text;
}